<?php namespace App\Http\Controllers;

use App\AboutUs;
use App\ApplicationField;
use App\Banner;
use App\Classify;
use App\FooterMenu;
use App\GoodsProduct;
use App\Http\Controllers\Api\UCenterController;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\Http\Services\Wechat\WechatLogin;
use App\HumanResources;
use App\Images;
use App\News;
use App\SysInfo;
use App\User;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

class HomeController extends Controller
{
    protected $webInfo    = null;//统一数据输出
    protected $title      = null;
    protected $footerMenu = null;//底部主要内容

    /**
     * 设置头部信息
     * @param $title
     */
    public function setTitle($title){
        $this->title = $title;
    }

    /**
     * 获取头部信息
     * @return null
     */
    public function getTitle(){
        return $this->title;
    }

    public function __construct(){
        $this->getWebInfo();
    }

    public function getWebInfo(){
        $webInfoArr = null;

        /** @var SysInfo $webInfo */
        $allWebInfo = SysInfo::all();
        if(count($allWebInfo) > 0){
            foreach($allWebInfo as $webInfo){
                switch($webInfo->type){
                    case 'title':
                        $webInfoArr['title'] = $webInfo->content;
                        break;
                    case 'logo':
                        $webInfoArr['logo_url'] = FileManager::getImgUrl((int)$webInfo->content);
                        break;
                    case 'url':
                        $webInfoArr['url'] = $webInfo->content;
                        break;
                    case 'footer':
                        $webInfoArr['footer_info'] = $webInfo->content;
                        break;
                }
            }
        }

        if(!is_null($this->getTitle())){
            $webInfoArr['title'] = $this->getTitle();
        }

        $bannerArr = array();
        /** @var Banner $banner */
        $allBanner = Banner::all();
        if(count($allBanner) > 0){
            foreach($allBanner as $banner){
                $bannerUrl = FileManager::getImgUrl($banner->img_id);
                array_push($bannerArr,$bannerUrl);
            }
        }

        $webInfoArr['banner'] = $bannerArr;

        $this->webInfo = $webInfoArr;

        //TODO 获取底部主要内容
        /** @var FooterMenu $footerMenu */
        $allFooterMenu = FooterMenu::wherePid(0)->get();
        if(count($allFooterMenu) > 0){
            foreach($allFooterMenu as $footerMenu){
                $childFooterMenu = FooterMenu::wherePid($footerMenu->id)->get();
                $footerMenu->child = $childFooterMenu;
            }
        }

        $this->footerMenu = $allFooterMenu;

    }


    public function index(Request $request){
        $select = 'index';

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var News $news */
        $indexNews = News::whereIsShow(1)->orderBy('created_at','desc')->limit(3)->get();
        if(count($indexNews) > 0){
            foreach($indexNews as $news){
                $news->thumb_img_url = FileManager::getImgUrl($news->thumb_img_id);
            }
        }

        /** @var GoodsProduct $product */
        $indexProduct = GoodsProduct::whereIsShop(1)->whereClassifyId(10)->orderBy('sort','asc')->limit(4)->get();
        $indexImg = null;
        if(count($indexProduct) > 0){
            foreach($indexProduct as $product){
                if($product->sort == 1){
                    $indexImg = FileManager::getImgUrl($product->thumb_id);
                }

                $product->thumb_url = FileManager::getImgUrl($product->thumb_id);
            }
        }

        /** @var ApplicationField $applicationField */
        $applicationFieldList = ApplicationField::orderBy('created_at','desc')->take(3)->get();
        if(count($applicationFieldList) > 0){
            foreach($applicationFieldList as $applicationField){
                $applicationField->thumb_img_url = FileManager::getImgUrl($applicationField->thumb_img_id);
                $applicationField->img_url       = FileManager::getImgUrl($applicationField->img_id);
            }
        }

        $indexJoin = HumanResources::whereIsShow(1)->orderBy('created_at','desc')->first();

        return view('index',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies,'indexNews' => $indexNews,'indexProduct' => $indexProduct,'indexImg' => $indexImg,'indexJoin' => $indexJoin,'applicationFieldList' => $applicationFieldList]);
    }

    public function applicationField(Request $request){
        $select = 'huge';
        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $applicationField = ApplicationField::whereId($id)->first();

        return view('application_field',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies,'applicationField' => $applicationField]);

    }

    public function about(Request $request){
        $select = 'about';

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $defaultClassify */
        $defaultClassify = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($defaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $defaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        $aboutUs = AboutUs::whereClassifyId($classifyId)->whereIsShow(1)->first();

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        return view('about',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'aboutUs' => $aboutUs,'classifyId' => $classifyId,'classifyName' => $classifyName,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }

    public function book(Request $request){
        $select = 'book';

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $newsClassify */
        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('book',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }

    public function contact(Request $request){
        $select = 'contact';

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $newsClassify */
        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('contact',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }

    public function friend(Request $request){
        $select = 'friend';

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('friend',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }

    public function join(Request $request){
        $select = 'join';

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $defaultClassify */
        $defaultClassify = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($defaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $defaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        $join = HumanResources::whereClassifyId($classifyId)->whereIsShow(1)->first();

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        return view('join',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'join' => $join,'classifyName' => $classifyName,'classifyId' => $classifyId,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }

    public function news(Request $request){
        $select = 'news';

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $newsClassify */
        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $defaultClassify */
        $defaultClassify = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($defaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $defaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        /** @var News $new */
        $news = News::whereClassifyId($classifyId)->whereIsShow(1)->orderBy('created_at','desc')->paginate(9);
        if(count($news) > 0){
            foreach($news as $new){
                $new->thumb_img_url = FileManager::getImgUrl($new->thumb_img_id);
            }
        }

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        return view('news',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'news' => $news,'classifyId' => $classifyId,'classifyName' => $classifyName,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }

    public function newsDetails(Request $request){
        $select = 'news';

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $newsClassify */
        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $defaultClassify */
        $defaultClassify = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($defaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $defaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        $newsId = !is_null($request->get('news_id')) ? $request->get('news_id') : 0;

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        $thisNew = News::whereId($newsId)->whereIsShow(1)->first();

        return view('news_details',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'thisNew' => $thisNew,'classifyId' => $classifyId,'classifyName' => $classifyName,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }

    public function product(Request $request){
        $select = 'product';

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $defaultClassify */
        $defaultClassify = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($defaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $defaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        /** @var GoodsProduct $product */
        $products = GoodsProduct::whereClassifyId($classifyId)->whereIsShop(1)->orderBy('sort','asc')->orderBy('created_at','desc')->paginate(10);
        if(count($products) > 0){
            foreach($products as $product){
                $product->thumb_url = FileManager::getImgUrl($product->thumb_id);
            }
        }

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        return view('product',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'classifyId' => $classifyId,'classifyName' => $classifyName,'products' => $products,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }

    public function productDetails(Request $request){
        $select = 'product';

        $joinClassifies = Classify::whereType('join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $aboutUsClassifies = Classify::whereType('about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $newsClassifies = Classify::whereType('news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $productClassifies = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $defaultClassify */
        $defaultClassify = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($defaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $defaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        $productId = !is_null($request->get('product_id')) ? $request->get('product_id') : 0;

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        $thisProduct = GoodsProduct::whereId($productId)->whereIsShop(1)->first();

        return view('product_details',['select' => $select,'web_info' => $this->webInfo,'footer_menu' => $this->footerMenu,'classifyId' => $classifyId,'classifyName' => $classifyName,'thisProduct' => $thisProduct,'newsClassifies' => $newsClassifies,'aboutUsClassifies' => $aboutUsClassifies,'joinClassifies' => $joinClassifies,'productClassifies' => $productClassifies]);
    }
}